import java.util.HashMap;
import java.util.StringTokenizer;


class P10371_39643 {
   
    static String[] tzs = {
    "UTC",
    "GMT",
    "BST",
    "IST",
    "WET",
    "WEST",
    "CET",
    "CEST",
    "EET",
    "EEST",
    "MSK",
    "MSD",
    "AST",
    "ADT",
    "NST",
    "NDT",
    "EST",
    "EDT",
    "CST",
    "CDT",
    "MST",
    "MDT",
    "PST",
    "PDT",
    "HST",
    "AKST",
    "AKDT",
    "AEST",
    "AEDT",
    "ACST",
    "ACDT",
    "AWST",};
   
    static int[][] offset = {
            {0,0},
            {0,0},
            {1,0},
            {1,0},
            {0,0},
            {1,0},
            {1,0},
            {2,0},
            {2,0},
            {3,0},
            {3,0},
            {4,0},
            {-4,0},
            {-3,0},
            {-3,-30},
            {-2,-30},
            {-5,0},
            {-4,0},
            {-6,0},
            {-5,0},
            {-7,0},
            {-6,0},
            {-8,0},
            {-7,0},
            {-10,0},
            {-9,0},
            {-8,0},
            {10,0},
            {11,0},
            {9,30},
            {10,30},
            {8,0},
    };

    static HashMap<String, int[]> maptz;
   
    public static void main(String[] args) {
       
        maptz = new HashMap<String, int[]>();
       
        for (int k = 0; k < tzs.length; k++) {
            maptz.put(tzs[k], offset[k]);
        }
       
        int cases = Integer.parseInt(readLn());
       
        for (int k = 0; k < cases; k++) {
           
            StringTokenizer st = new StringTokenizer(readLn());
           
            String first = st.nextToken();
           
            int h = 0;
            int m = 0;
           
            String tz1, tz2;
            if ("noon".equals(first)) {
                h = 12;
            } else if ("midnight".equals(first)) {
                h = 0;
                m = 0;
               
            } else {
               
                StringTokenizer st1 = new StringTokenizer(first, ":");
               
                h = Integer.parseInt(st1.nextToken());
                m = Integer.parseInt(st1.nextToken());
               
                String period = st.nextToken();
               
                if ("p.m.".equals(period) && h != 12) {
                    h += 12;
                }
               
                if ("a.m.".equals(period) && h == 12) {
                    h = 0;
                }
               
            }
           
            tz1 = st.nextToken();
            tz2 = st.nextToken();
           
//            System.out.println(h + ":" + m);
           
            int[] timeInUTC = toUTC(h, m, tz1);
           
//            System.out.println("In UTC: " + timeInUTC[0] + ":" + timeInUTC[1]);
           
            int[] timeInTZ2 = fromUTCtoTZ(timeInUTC[0], timeInUTC[1], tz2);
           
//            System.out.println("In TZ2: " + timeInTZ2[0] + ":" + timeInTZ2[1]);
           
            if (timeInTZ2[0] == 0 && timeInTZ2[1] == 0) {
                System.out.println("midnight");
            } else if (timeInTZ2[0] == 12 && timeInTZ2[1] == 0){
                System.out.println("noon");
            } else {
                if (timeInTZ2[0] > 12) {
                    System.out.println((timeInTZ2[0] - 12) + ":" + (timeInTZ2[1] > 9 ? "" : "0") +  timeInTZ2[1] + " p.m.");
                } else if (timeInTZ2[0] == 12) {
                    System.out.println((timeInTZ2[0]) + ":" + (timeInTZ2[1] > 9 ? "" : "0") + timeInTZ2[1] + " p.m.");
                } else{
                    if (timeInTZ2[0] == 0) {
                        timeInTZ2[0] = 12;
                    }
                    System.out.println((timeInTZ2[0]) + ":" + (timeInTZ2[1] > 9 ? "" : "0") + timeInTZ2[1] + " a.m.");
                }
            }
        }
       
       
    }
   
    private static int[] toUTC(int h, int m, String tz1) {
        int[] time = new int[2];
       
        time[0] = h;
        time[1] = m;
       
        int[] offset = maptz.get(tz1);
       
        time[0] = time[0] - offset[0];
        time[1] = time[1] - offset[1];
       
        if (time[1] >= 60) {
            time[1] = time[1] - 60;
            time[0] = time[0] + 1;
        } else if (time[1] < 0){
            time[1] = time[1] + 60;
            time[0] = time[0] - 1;
        }
       
        if (time[0] >= 24) {
            time[0] = time[0] - 24;
        } else if (time[0] < 0) {
            time[0] = time[0] + 24;
        }
       
        return time;
    }

    private static int[] fromUTCtoTZ(int h, int m, String tz2) {
        int[] time = new int[2];
       
        time[0] = h;
        time[1] = m;
       
        int[] offset = maptz.get(tz2);
       
        time[0] = time[0] + offset[0];
        time[1] = time[1] + offset[1];
       
        if (time[1] >= 60) {
            time[1] = time[1] - 60;
            time[0] = time[0] + 1;
        } else if (time[1] < 0){
            time[1] = time[1] + 60;
            time[0] = time[0] - 1;
        }
       
        if (time[0] >= 24) {
            time[0] = time[0] - 24;
        } else if (time[0] < 0) {
            time[0] = time[0] + 24;
        }
       
        return time;
    }
   
    static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuffer buffer = new StringBuffer();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char) car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
                System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) {
            return (null);
        }
        if ((car < 0) && (buffer.length() == 0))
            return (null);
        return (buffer.toString()).trim();
    }
   
}